<template>
  <div class="body">
    <div>
      <p class="web_title">
        {{ form.transaction_type == "recharge" ? "机构充值" : "机构退款" }}
      </p>
    </div>

    <el-form class="form" :model="form" ref="refForm">
      <el-form-item
        label="商务"
        :label-width="fromViewCofing.label_width"
        v-if="organization.user"
      >
        <el-input
          v-model="organization.business.nickname"
          :disabled="true"
          class="inp"
        ></el-input>
      </el-form-item>

      <el-form-item label="机构" :label-width="fromViewCofing.label_width">
        <el-input
          v-model="organization.name"
          :disabled="true"
          class="inp"
        ></el-input>
      </el-form-item>

      <el-form-item
        :rules="[{ required: true, message: '金额不能为空' }]"
        label="金额"
        :label-width="fromViewCofing.label_width"
      >
        <el-input v-model="form.money" class="inp"></el-input>
      </el-form-item>

      <el-form-item
        v-if="form.transaction_type == 'recharge'"
        :rules="[{ required: true, message: '充值类型不能为空' }]"
        label="充值类型"
        :label-width="fromViewCofing.label_width"
      >
        <el-radio-group v-model="form.recharge_type">
          <el-radio label="receipts">充值</el-radio>
          <el-radio label="replenish">补款</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="凭证图片" :label-width="fromViewCofing.label_width">
        <el-upload
          class="upload"
          :multiple="true"
          :action="uploading_img.api_uploading_img_url"
          :headers="uploading_img.headers"
          :limit="4"
          :show-file-list="false"
          :on-success="
            (res, file) => {
              handleUploadingImg('voucher_img', res, file);
            }
          "
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <span slot="tip" class="el-upload__tip tip"
            >只能上传jpg/png文件，且不超过2MB</span
          >
        </el-upload>

        <div style="display: flex; flex-wrap: wrap">
          <el-image
            v-for="img in form.voucher_img"
            style="width: 100px; height: 100px; margin-right: 5px"
            :src="imgShow(img)"
            fit="cover"
          ></el-image>
        </div>
      </el-form-item>

      <el-form-item label="备注" :label-width="fromViewCofing.label_width">
        <el-input
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 16 }"
          v-model="form.remark"
          class="textarea"
        ></el-input>
      </el-form-item>

      <div class="form_footer">
        <el-button type="success" @click="fromSubmit('refForm')"
          >提交</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import {
  apiOrganizationDetails,
  apiOrganizationTransaction,
} from "@/api/apiOrganization";
import {
  getUploadingImgUrl,
  getUploadingVideoUrl,
  getImgUrl,
} from "@/api/uploading";
import { arrayToString, stringToArray } from "@/utils/arrayUtil";
import { getToken } from "@/utils/auth";
import {
  apiAgentList,
  apiGroupList,
  apiAgentCreate,
  apiAgentUpdate,
} from "@/api/apiCommon";
export default {
  name: "Index",
  data() {
    return {
      init_form: {
        o_id: 0,
        money: "",
        voucher_img: [],
        remark: "",
        transaction_type: "recharge",
        recharge_type: "receipts",
      },
      form: {},
      uploading_img: {
        headers: {},
        api_uploading_img_url: "",
        api_uploading_video_url: "",
        dialogImageUrl: "",
        dialogImgVisible: false,
        disabled: false,
        fileList: [],
      },

      fromViewCofing: {
        input_width: "120px",
        label_width: "120px",
      },
      business_list: [],
      organization: {
        name: "",
      },
    };
  },
  created() {
    this.form = this.init_form;
    var params = this.$route.query;
    if (params.id != 0) {
      this.form.o_id = params.id;
      this.form.transaction_type = params.transaction_type;
      this.req_organization_details(params.id);
    }
    this.uploading_img.api_uploading_img_url = getUploadingImgUrl();
    this.uploading_img.api_uploading_video_url = getUploadingVideoUrl();
    this.uploading_img.headers = {
      token: getToken(),
    };
  },
  methods: {
    //图片展示
    imgShow(url) {
      return getImgUrl(url);
    },
    // 图片上传成功
    handleUploadingImg(field, res, file) {
      console.log([res, file, field]);
      if (res.code == 1) {
        var form = this.form;
        form[field].push(res.data.path);
        console.log(form, "formformformform");
        this.form = form;
        this.$forceUpdate();
      } else {
        this.$message.error(res.msg);
      }
    },
    // 图片删除
    handleRemoveLogo(file) {
      console.log("图片删除");
      var form = this.form;
      form["logo_img"] = "";
      this.form = form;
      // this.$refs.ref_uploadImg.$children[1].$el.style.display = ''
      this.$forceUpdate();
      console.log(file);
    },
    /**表单提交 */
    fromSubmit(formName) {
      const _this = this;

      var regex = /^-?\d+(\.\d{1,2})?$/;
      if (!regex.test(_this.form.money)) {
        _this.$message.error("金额格式错误");
        return false;
      }

      this.$confirm("是否确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });

          var form = _this.form;
          var reqData = {};
          reqData.o_id = form.o_id;
          reqData.transaction_type = form.transaction_type;
          reqData.money = form.money;
          reqData.remark = form.remark;
          reqData.recharge_type = form.recharge_type;
          reqData.voucher_img = arrayToString(form.voucher_img);

          apiOrganizationTransaction(reqData).then((response) => {
            loading.close();
            _this.$message({
              message: response.msg,
              type: "success",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //获取商务人员
    req_agent() {
      const _this = this;
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      apiAgentList({
        page: 1,
        size: 99,
        status: "normal",
        group_id: 3,
      })
        .then((response) => {
          const result = response.data.datalist.data;
          _this.business_list = result;
          loading.close();
        })
        .catch(function (result) {
          _this.$message.error(result.msg);
        });
    },
    // 获取详情
    req_organization_details(id) {
      if (!id) {
        return;
      }
      const _this = this;
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      apiOrganizationDetails({
        id: id,
      })
        .then((response) => {
          const result = response.data.details;
          _this.organization = result;
          loading.close();
        })
        .catch(function (result) {
          _this.$message.error(result.msg);
        });
    },
  },
};
</script>

<style lang="scss">
.body {
 
  .title {
    font-size: 36px;
  }

  .form {
    width: 100%;
    height: 100%;

    .inp {
      width: 280px;
    }

    .textarea {
      width: 380px;
    }

    .upload {
      .tip {
        margin-left: 10px;
        color: #616161;
      }
    }

    .form_footer {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      background-color: #616161;
      padding: 10px;
      text-align: center;
    }
  }
}
</style>
